package com.hnkywl.sts.dao.jsgl;


import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Jsdhb;
import com.hnkywl.sts.entity.organ.Qyjg;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:59
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class JsdhbDao extends SimpleHibernateTemplate<Jsdhb,Long> {
    public Page<Jsdhb> findfphbList(String hbrName,String voucherNum,Long gsId,String gsJc,Date hbrqStart,Date hbrqEnd,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jsdhb> page = new Page<Jsdhb>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (hbrName != null && !hbrName.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" hbr.xm like ? ");
            param.add("%" + hbrName.trim() + "%");
        }
        if (voucherNum != null && !voucherNum.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" voucherNum = ? ");
            param.add(voucherNum);
        }
        if (hbrqStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" hbrq >= ?");
            param.add(hbrqStart);
        }
        if (hbrqEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" hbrq <= ?");
            param.add(hbrqEnd);
        }
//        if (glqyQc != null && !"".equals(glqyQc)) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" glqy.qc like ? ");
//            param.add("%" + glqyQc.trim() + "%");
//        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.id = ? ");
            param.add(gsId);
        }
        if (gsJc != null && !"".equals(gsJc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.jc like ? ");
            param.add("%" + gsJc.trim() + "%");
        }
//        if (fpzlId != null) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" fpzl = ? ");
//            param.add(fpzlId);
//        }
//        if (type != null) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            condition.append(" type = ? ");
//            param.add(type);
//        }
//        if (condition.length() != 0)
//            condition.append(" and ");
//        condition.append(" zt = ? ");
//        param.add(1);
//        if (condition.length() != 0)
//            condition.append(" and ");
//        condition.append(" shzt = ? ");
//        param.add(1);
//        if (condition.length() != 0)
//            condition.append(" and ");
//        condition.append(" Jsdhbhb.id is null ");
        //==============记录数量===============
        hql = "select count(*) from Jsdhb where 1=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jsdhb where 1=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
    
    public Integer getMaxfpPxh() {
        String hql = "select MAX(hbdh) from Jsdhb";
        return findInt(hql);
    }

}
